//
//  PersonalCenterMessageViewController.m
//  XingMei
//
//  Created by 姚琪 on 2019/3/25.
//  Copyright © 2019年 钱金军. All rights reserved.
//

#import "PersonalCenterMessageViewController.h"
#import "PersonalCenterMessageTableViewCell.h"

@interface PersonalCenterMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *announcementTableView;

@property (nonatomic,strong) NSMutableArray *announcementArr;



@end

@implementation PersonalCenterMessageViewController{
    int page;
    UIImageView *errimg;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.announcementArr = [[NSMutableArray alloc] init];
    [self setNavTitleText:@"消息"];
    self.HeaderView.backgroundColor = rgb(244,244,244);
    [self layout];
}
- (void)layout{
    _announcementTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, Height_NavBar, KScreenWidth, KScreenHeight - Height_NavBar - Height_Indicator) style:UITableViewStylePlain];
    _announcementTableView.delegate = self;
    _announcementTableView.dataSource = self;
    _announcementTableView.tableFooterView = [[UIView alloc] init];
    _announcementTableView.showsVerticalScrollIndicator = NO;
//    _announcementTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headData)];
//    [_announcementTableView.mj_header beginRefreshing];
//    _announcementTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
//    _announcementTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footData)];
    [self.view addSubview:_announcementTableView];
    errimg = [self addNoneGoodslist];
    errimg.centerXv = _announcementTableView.width/2;
    errimg.centerYv = _announcementTableView.height/2;
    [_announcementTableView addSubview:errimg];
}
#pragma mark 上下拉刷新
-(void)headData{
    page = 1;
    [self getSystemAnnouncement:1];
}

-(void)footData{
    page ++ ;
    [self getSystemAnnouncement:0];
}

#pragma mark tableView DelegateAndDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.announcementArr.count;
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalCenterMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[PersonalCenterMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
//    cell.dict = self.announcementArr[indexPath.row];
    cell.dict = [NSDictionary dictionary];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
    return headerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    AnnouncementDetailsViewController *details = [[AnnouncementDetailsViewController alloc] init];
//    details.content = self.announcementArr[indexPath.row][@"content"];
//    [self.navigationController pushViewController:details animated:YES];
}

#pragma mark 获取公告列表
- (void)getSystemAnnouncement:(int)tag{
    NSDictionary *dic = @{
                          @"pagenum":[NSString stringWithFormat:@"%d",page],
                          @"pagesize":@"10",
                          };
    
    [ZWJNetworkManager requestWithRequestData:dic WithUrl:@"" WithWithCompletionBlockWithSuccess:^(NSDictionary *dic) {
        [self.announcementTableView.mj_header endRefreshing];
        [self.announcementTableView.mj_footer endRefreshing];
        
        
        if ([dic[Code] integerValue] == 200) {
            if (dic[Response] == nil) {
                self.announcementTableView.hidden = YES;
            }else{
                self.announcementTableView.hidden = NO;
            }
            if (tag == 1) {
                self.announcementArr = [[NSMutableArray alloc] init];
            }
            
            [self.announcementArr addObjectsFromArray:dic[Response]];
            if (self.announcementArr.count > 0) {
                self->errimg.hidden = YES;
            }else{
                self->errimg.hidden = NO;
            }
            
        }else{
            [self showWithTitle:dic[Msg]];
        }
        [self.announcementTableView reloadData];
    } failure:^(id error) {
        [self.announcementTableView.mj_header endRefreshing];
        [self.announcementTableView.mj_footer endRefreshing];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
